<?php
class AcosController extends AppController{
    
    
        function beforeFilter() {
		parent::beforeFilter();
		$this->Auth->allowedActions = array('index', 'view');
	}
	
	function index(){
		
	}
	
	function view(){
		
	}
        
	function getNode($id_parent = null){                
		$this->set('node',$this->getChilds($id_parent));
	
	}
        
        
        
	// Fonction récursive renvoyant tous les descendants d'un noeud d'un arbre
	function getChilds($id_parent = null){
		//xdebug_break();
		$noeuds_extraits = $this->Aco->find('all', array(
		'conditions' => array('parent_id' => $id_parent),
		'fields' => array('id', 'alias'),
		'recursive'=> -1,
		'order' => 'alias DESC'));
	
		$noeuds = array();
		//$children = array();
		
		$n = 0;

		foreach($noeuds_extraits as $cle => $valeur){
			
				/*$nb_fils = $this->Aco->find('count', array(
						'conditions' => array('parent_id' => $valeur['Aco']['id'])));
				


				if ($nb_fils > 0){
						$cond = 'false';
						$children = $this->getChilds($valeur['Aco']['id']);
				}else{
						$cond = 'true';
						$children = null;
				}*/
				$children = $this->getChilds($valeur['Aco']['id']);
				if ($children == null){
					$cond = 'true';
				}  else {
					$cond = 'false';
				}
					
			

				$noeud = array(
						'text' => $valeur['Aco']['alias'],
						'id' => $valeur['Aco']['id'], 
						'leaf' => $cond,
						'iconCls' => 'icon-application_stop',
						'children' => $children);

				$noeuds[$n] = $noeud ;
				$n++;
		}
		
		return $noeuds;

		
	}
    
    
}
?>
